3 use MediaWiki\Linker\LinkRenderer
;
4 use MediaWiki\MediaWikiServices
;
7 * @covers MediaWiki\Linker\LinkRenderer
9 class LinkRendererTest
extends MediaWikiLangTestCase
{
14 private $titleFormatter;
16 public function setUp() {
18 $this->setMwGlobals( [
19 'wgArticlePath' => '/wiki/$1',
20 'wgServer' => '//example.org',
21 'wgCanonicalServer' => 'http://example.org',
22 'wgScriptPath' => '/w',
23 'wgScript' => '/w/index.php',
25 $this->titleFormatter
= MediaWikiServices
::getInstance()->getTitleFormatter();
28 public function testMergeAttribs() {
29 $target = new TitleValue( NS_SPECIAL
, 'Blankpage' );
30 $linkRenderer = new LinkRenderer( $this->titleFormatter
);
31 $link = $linkRenderer->makeBrokenLink( $target, null, [
34 // Suppresses href attribute
40 '<a href="/wiki/Special:BlankPage" class="new foobar" '
41 . 'title="Special:BlankPage (page does not exist)" bar="baz">'
42 . 'Special:BlankPage</a>',
47 public function testMakeKnownLink() {
48 $target = new TitleValue( NS_MAIN
, 'Foobar' );
49 $linkRenderer = new LinkRenderer( $this->titleFormatter
);
53 '<a href="/w/index.php?title=Foobar&foo=bar" '. 'title="Foobar">Foobar</a>',
54 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
58 $linkRenderer->setForceArticlePath( true );
60 '<a href="/wiki/Foobar?foo=bar" title="Foobar">Foobar</a>',
61 $linkRenderer->makeKnownLink( $target, null, [], [ 'foo' => 'bar' ] )
65 $linkRenderer->setForceArticlePath( false );
66 $linkRenderer->setExpandURLs( PROTO_HTTPS
);
68 '<a href="https://example.org/wiki/Foobar" title="Foobar">Foobar</a>',
69 $linkRenderer->makeKnownLink( $target )
73 public function testMakeBrokenLink() {
74 $target = new TitleValue( NS_MAIN
, 'Foobar' );
75 $special = new TitleValue( NS_SPECIAL
, 'Foobar' );
76 $linkRenderer = new LinkRenderer( $this->titleFormatter
);
78 // action=edit&redlink=1 added
80 '<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
81 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
82 $linkRenderer->makeBrokenLink( $target )
85 // action=edit&redlink=1 not added due to action query parameter
87 '<a href="/w/index.php?title=Foobar&action=foobar" class="new" '
88 . 'title="Foobar (page does not exist)">Foobar</a>',
89 $linkRenderer->makeBrokenLink( $target, null, [], [ 'action' => 'foobar' ] )
92 // action=edit&redlink=1 not added due to NS_SPECIAL
94 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
95 . '(page does not exist)">Special:Foobar</a>',
96 $linkRenderer->makeBrokenLink( $special )
101 '<a href="/w/index.php?title=Foobar&action=edit&redlink=1" '
102 . 'class="new" title="Foobar (page does not exist)">Foobar</a>',
103 $linkRenderer->makeBrokenLink( $target->createFragmentTarget( 'foobar' ) )
107 public function testMakeLink() {
108 $linkRenderer = new LinkRenderer( $this->titleFormatter
);
109 $foobar = new TitleValue( NS_SPECIAL
, 'Foobar' );
110 $blankpage = new TitleValue( NS_SPECIAL
, 'Blankpage' );
112 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
113 . '(page does not exist)">foo</a>',
114 $linkRenderer->makeLink( $foobar, 'foo' )
118 '<a href="/wiki/Special:BlankPage" title="Special:BlankPage">blank</a>',
119 $linkRenderer->makeLink( $blankpage, 'blank' )
123 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
124 . '(page does not exist)"><script>evil()</script></a>',
125 $linkRenderer->makeLink( $foobar, '<script>evil()</script>' )
129 '<a href="/wiki/Special:Foobar" class="new" title="Special:Foobar '
130 . '(page does not exist)"><script>evil()</script></a>',
131 $linkRenderer->makeLink( $foobar, new HtmlArmor( '<script>evil()</script>' ) )